﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace KStarProxy
{
    class Program
    {
       
        static void Main(string[] args)
        {
            KStarProxy proxy = KStarProxy.GetInstance();

            while (true) 
            {
                string comm = Console.ReadLine();
                if (comm == "start")
                {
                    proxy.Start();
                }
                else if (comm == "stop")
                {
                    proxy.Stop();
                }
            }


            //try
            //{
            //    start();
            //    byte[] data;
            //    int count = getQuotBytes(out data);
            //    if (count > 0)
            //        saveQuotBytes(data);
            //    stop();
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.StackTrace);
            //}
            //Console.Read();
        }
               
        private static int getQuotBytes(out byte[] data)
        {
            StringBuilder msg = new StringBuilder(256);
            data = new byte[400 * 50];
            int count = KStarAPI.KSFTHQPUB_GetQuota_Byte(data, 400 * 50, 2000, msg);
            if (count > 0)
            {
                return count;
            }
            else if (count < 0) //出错了
            {
                throw new Exception(msg.ToString());
            }
            else //无数据
            {
                //return data;
                return count;
            }
        }
        private static void saveQuotBytes(byte[] data)
        {
            File.WriteAllBytes("kstar.dat", data);
        }
        private static DepthMarketData convertRecord(KStarAPI.KSFT_QUOTA_PUBDATA_ITEM kstarRecord)
        {
            DepthMarketData rtn = new DepthMarketData()
            {
                askPrice1 = kstarRecord.askPrice1,
                askPrice2 = kstarRecord.askPrice2,
                askPrice3 = kstarRecord.askPrice3,
                askPrice4 = kstarRecord.askPrice4,
                askPrice5 = kstarRecord.askPrice5,
                askVolume1 = kstarRecord.askVolume1,
                askVolume2 = kstarRecord.askVolume2,
                askVolume3 = kstarRecord.askVolume3,
                askVolume4 = kstarRecord.askVolume4,
                askVolume5 = kstarRecord.askVolume5,

                bidPrice1 = kstarRecord.bidPrice1,
                bidPrice2 = kstarRecord.bidPrice2,
                bidPrice3 = kstarRecord.bidPrice3,
                bidPrice4 = kstarRecord.bidPrice4,
                bidPrice5 = kstarRecord.bidPrice5,
                bidVolume1 = kstarRecord.bidVolume1,
                bidVolume2 = kstarRecord.bidVolume2,
                bidVolume3 = kstarRecord.bidVolume3,
                bidVolume4 = kstarRecord.bidVolume4,
                bidVolume5 = kstarRecord.bidVolume5,

                lastPrice = kstarRecord.lastPrice,
                closePrice = kstarRecord.closePrice,

                openPrice = kstarRecord.openPrice,
                highestPrice = kstarRecord.highestPrice,
                lowestPrice = kstarRecord.lowestPrice,
                openInterest = kstarRecord.openInterest,
                volume = kstarRecord.doneVolume,//不确定

                preClosePrice = kstarRecord.preClosePrice,
                preSettlementPrice = kstarRecord.preSettlePrice,
                preOpenInterest = kstarRecord.preOpenInterest,

                upperLimitPrice = kstarRecord.upperLimitPrice,
                lowerLimitPrice = kstarRecord.lowerLimitPrice,
                turnover = kstarRecord.turnover,
                preDelta = kstarRecord.preDelta,
                currDelta = kstarRecord.currDelta,
                settlementPrice = kstarRecord.settlePrice,
                tradingDay = kstarRecord.deliv_date //不确定


            };
            return rtn;
        }

    }

}
